﻿using RabbitMQ.Client;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

var factory = new ConnectionFactory();
factory.HostName = "172.30.14.227";//RabbitMQ服务器地址
factory.Port = 5672;
factory.UserName = "root";
factory.Password = "123456";
factory.DispatchConsumersAsync = true;
string exchangeName = "NuctechEventLogs";//交换机的名字
string eventName = "SMS.SentLog";// routingKey的值
string queueName = "EventLogs";// 队列名称
using var conn = factory.CreateConnection();
int i = 0;
while (i <= 10)
{
    //待发送消息
    EventCreatePostDto eventCreatePostDto = new EventCreatePostDto()
    {
        StationType = StationTypeEnum.SttSmsc,
        UserId = 2,
        EventLevel = EventLevelEnum.Info,
        EventSubType = EventSubTypeEnum.Login,
        DesTemplateId = 137740943366,
        CreateTime = DateTime.UtcNow,
        ErrCode = i,
        Des = JsonSerializer.Serialize(
                    new EventKeyInfoDto
                    {
                        KeyInfos = new List<KeyInfo>
                        {
                                            new KeyInfo
                                            {
                                                Index = 0,
                                                NeedTranslation = false,
                                                Params = i.ToString(),
                                            }
                        }
                    }),
    };

    using (var channel = conn.CreateModel())//创建信道
    {
        var properties = channel.CreateBasicProperties();
        // 将消息标记为持久化消息。
        properties.DeliveryMode = 2;
        // 声明交换机；交换机在 RabbitMQ 服务器重启后仍然存在： durable: true
        channel.ExchangeDeclare(exchange: exchangeName, type: "direct", durable: true);
        byte[] body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(eventCreatePostDto));

        // 声明队列并绑定
        channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false);
        channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: eventName);

        //
        channel.BasicPublish(exchange: exchangeName, routingKey: eventName,
            mandatory: true, basicProperties: properties, body: body);//发布消息
    }
    Console.WriteLine("第" + i + "次发布了消息:" + JsonSerializer.Serialize(eventCreatePostDto));
    Thread.Sleep(1000);
    i++;
}

public record EventCreatePostDto
{
    [JsonPropertyName("station_type")]
    public StationTypeEnum StationType { get; set; }

    [JsonPropertyName("station_name")]
    public string? StationName { get; set; }

    [JsonPropertyName("user_id")]
    public int UserId { get; set; }

    [JsonPropertyName("event_level")]
    public EventLevelEnum EventLevel { get; set; }

    [JsonPropertyName("event_sub_type")]
    public EventSubTypeEnum EventSubType { get; set; }

    [JsonIgnore]
    public string? Des { get; set; }

    [JsonPropertyName("KeyInfo")]
    public EventKeyInfoDto? KeyInfos { get; set; }

    [JsonPropertyName("des_template_id")]
    public long DesTemplateId { get; set; }

    [JsonPropertyName("create_time")]
    public DateTime? CreateTime { get; set; }

    [JsonPropertyName("errcode")]
    public long ErrCode { get; set; }

    [JsonIgnore]
    public string? ZhCn { get; set; }

    [JsonIgnore]
    public string? ZhHk { get; set; }

    [JsonIgnore]
    public string? EnUs { get; set; }
}

public enum StationTypeEnum
{
    SttUnspecified = 0,
    SttUnknown = -1,

    /// <summary>
    /// 判图工作站
    /// </summary>
    SttIps = 1,

    /// <summary>
    /// 扫描控制服务
    /// </summary>
    SttScs = 2,

    /// <summary>
    /// 重建服务
    /// </summary>
    SttRcc = 3,

    /// <summary>
    /// 识别服务
    /// </summary>
    SttRcg = 4,

    /// <summary>
    /// 系统管理服务
    /// </summary>
    SttSms = 5,

    /// <summary>
    /// 存储服务
    /// </summary>
    SttRcs = 6,

    /// <summary>
    /// 存储服务
    /// </summary>
    SttCss = 7,

    /// <summary>
    /// 系统管理站
    /// </summary>
    SttVue = 8,

    /// <summary>
    /// 设备管理站
    /// </summary>
    SttOpt = 9,

    /// <summary>
    /// 维护管理站
    /// </summary>
    SttMta = 10,

    /// <summary>
    ///预检管理站
    /// </summary>
    SttPck = 11,

    /// <summary>
    ///开检工作站
    /// </summary>
    SttCbs = 12,

    /// <summary>
    ///判图服务
    /// </summary>
    SttIpss = 13,

    /// <summary>
    ///判图服务
    /// </summary>
    SttIpc = 14,

    /// <summary>
    ///确认工作站
    /// </summary>
    SttCfs = 15,

    /// <summary>
    ///设备管理服务
    /// </summary>
    SttDms = 16,

    /// <summary>
    ///系统管理站
    /// </summary>
    SttSmsc = 17,

    /// <summary>
    ///设备管理站
    /// </summary>
    SttDmsc = 18,

    /// <summary>
    /// 预检服务
    /// </summary>
    SttPiss = 19,

    /// <summary>
    /// 预检管理站
    /// </summary>
    SttPisc = 20
}

public record EventKeyInfoDto
{
    public List<KeyInfo> KeyInfos { get; set; } = new List<KeyInfo>();
}
public record KeyInfo
{
    public int Index { get; set; }

    public bool NeedTranslation { get; set; }

    public string? Params { get; set; }

    public List<int> ParamsList { get; set; } = new List<int>();

    public long ParamId { get; set; }

    /// <summary>
    /// Indicates the identification number, the default user group needs to be translated, and the permission needs to be translated
    ///1-- User group; 2- User; 3-- Permissions
    /// </summary>
    public TableMarkEnum TableMark { get; set; }
}

public enum EventLevelEnum
{
    Deadly = 1,       //致命
    Error = 2,        //错误
    Warning = 3,      //警告
    Info = 4,         //信息
    Debug = 5,        //调试
}

/// <summary>
///
/// </summary>
public enum EventSubTypeEnum
{
    Login = 1,                 //登入
    Logout = 2,                //登出
    Correction = 3,            //校正
    Reconstruction = 4,        //重建
    Identify = 5,              //识别
    Establish = 6,             //新建
    Delete = 7,                //删除
    Modify = 8,                //修改
    Query = 9,                 //查询
    Data_collection = 10,      //数采
    Export = 11,               //导出
    Device_error = 12,         //故障
    Recovery = 13,             //恢复-设备
    Maintain = 14,             //维护
    Diag = 15,                 //诊断
    Reset = 16,                //复位
    Check = 17,                //校验
    Sys_err = 18,              //异常
    Sys_recovery = 19,         //恢复-系统
    Judge_image = 20,          //判图--代码隐藏
    Over_flow = 21,            //溢出--代码隐藏
    Scanning = 22,             //扫描
    Print = 23,                //打印
    Import = 24,               //导入
    BoxSend = 25,              //箱体送出
    BoxBack = 26,              //箱体退回
    Twicercs = 27,             //再重建
    Twicercg = 28              //再识别
}

public enum TableMarkEnum
{
    UserGroup = 1,       //User group
    User = 2,            //User
    Permissions = 3      //Permissions
}